package zaijiansunwukong.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.codec.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.awt.image.BufferedImage.TYPE_INT_RGB;

/**
 * Created by Administrator on 2016/4/20.
 */
public class PdfUtil implements Serializable {

    static Logger logger = Logger.getLogger(PdfUtil.class);

    /**
     * html转pdf。html  最大尺寸790,1120
     * @param html
     * @param outputFile
     * @throws Exception
     */
    public static void htmlToPdf(String html,String outputFile) throws Exception{
        OutputStream os = null;
        try {
            os = new FileOutputStream(outputFile);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(html);
            // 解决中文问题
            //Arial Unicode MS   SimSun Microsoft YaHei
            String freemarkerFontPath = "/home/weblogic/data/web/sfs/freemarker";
            if (StringUtils.isBlank(freemarkerFontPath)) {
                logger.error("freemarkerFontPath is blank!");
                return;
            }
            String msyhPath = freemarkerFontPath + "/MSYH.TTF";
            File msyhFile = new File(msyhPath);
            if (!msyhFile.exists() || !FileUtil.isReadable(msyhFile)) {
                logger.error("MSYH.TTF does not exist or permission denied !");
            }
            String msyhbdPath = freemarkerFontPath + "/MSYHBD.TTF";
            File msyhbdFile = new File(msyhbdPath);
            if (!msyhbdFile.exists() || !FileUtil.isReadable(msyhbdFile)) {
                logger.error("MSYHBD.TTF does not exist or permission denied !");
            }
            ITextFontResolver fontResolver = renderer.getFontResolver();
            fontResolver.addFont(msyhPath, BaseFont.IDENTITY_H,
                    BaseFont.NOT_EMBEDDED);
            fontResolver.addFont(msyhbdPath, BaseFont.IDENTITY_H,
                    BaseFont.NOT_EMBEDDED);
            //renderer.getSharedContext().setBaseURL("http://192.168.0.138:8081/saferycom/images/");
            renderer.layout();
            renderer.createPDF(os);
            logger.info("转换成功！");
        } catch(Exception e) {
            logger.error("", e);
        } finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    logger.error("", e);
                }
            }
        }
    }
    /**
     * html转pdf。html  最大尺寸790,1120
     * @param html
     * @throws Exception
     */
    public static byte[] htmlToPdf(String html) throws Exception{
        ByteArrayOutputStream os = null;
        try {
            os = new ByteArrayOutputStream();

            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(html);
            // 解决中文问题
            //Arial Unicode MS   SimSun Microsoft YaHei
            String freemarkerFontPath = "/home/weblogic/data/web/sfs/freemarker";
            if (StringUtils.isBlank(freemarkerFontPath)) {
                logger.error("freemarkerFontPath is blank!");
                return null;
            }
            String msyhPath = freemarkerFontPath + "/MSYH.TTF";
            File msyhFile = new File(msyhPath);
            if (!msyhFile.exists() || !FileUtil.isReadable(msyhFile)) {
                logger.error("MSYH.TTF does not exist or permission denied !");
                return null;
            }
            String msyhbdPath = freemarkerFontPath + "/MSYHBD.TTF";
            File msyhbdFile = new File(msyhbdPath);
            if (!msyhbdFile.exists() || !FileUtil.isReadable(msyhbdFile)) {
                logger.error("MSYHBD.TTF does not exist or permission denied !");
                return null;
            }
            ITextFontResolver fontResolver = renderer.getFontResolver();
            fontResolver.addFont(msyhPath, BaseFont.IDENTITY_H,
                    BaseFont.NOT_EMBEDDED);
            fontResolver.addFont(msyhbdPath, BaseFont.IDENTITY_H,
                    BaseFont.NOT_EMBEDDED);
            //renderer.getSharedContext().setBaseURL("http://192.168.0.138:8081/saferycom/images/");
            renderer.layout();
            renderer.createPDF(os);
            logger.info("转换成功！");

            return os.toByteArray();
        } catch(Exception e) {
            logger.error("", e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    logger.error("", e);
                }
            }
        }

        return null;
    }


}
